package com.digitaldreamers.spaceshooter.scene;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;

import com.digitaldreamers.spaceshooter.gameobject.LaserBeam;
import com.digitaldreamers.spaceshooter.gameobject.PlayerShip;

public class GameScene extends BaseScene {
	private PlayerShip mPlayerShip;
	private HUD mGameHUD;

	@Override
	public Scene onLoadingSceneLoadAndShown() {
		return null;
	}

	@Override
	public void onLoadingSceneUnloadAndHidden() {
		
	}

	@Override
	public void onLoadScene() {
		this.mResourceManager.loadGameSceneResources();
		this.createPlayer();
		this.createHUD();
	}

	@Override
	public void onShowScene() {
		this.mPlayerShip.initialize();
	}

	@Override
	public void onHideScene() {
		
	}

	@Override
	public void onUnloadScene() {
		super.onUnloadScene();
		this.mResourceManager.unloadGameSceneResources();
		this.mCamera.setHUD(null);
	}
	
	private void createPlayer() {
		AnimatedSprite playerSpaceShipSprite = new AnimatedSprite(-1000, -1000, this.mResourceManager.mPlayerShipTextureRegion, this.mVBOM);
		this.mPlayerShip = new PlayerShip(this, playerSpaceShipSprite);
		this.mEngine.enableAccelerationSensor(this.mContext, this.mPlayerShip);
		this.attachChild(this.mPlayerShip);
	}
	
	private void createHUD() {
		this.mGameHUD = new HUD();
		this.mGameHUD.setTouchAreaBindingOnActionDownEnabled(true);
		this.mGameHUD.setTouchAreaBindingOnActionMoveEnabled(true);
		
		Text text = new Text(0, 0, this.mResourceManager.mMoonHouseFont, "Fire", this.mVBOM) {
			@Override
			public boolean onAreaTouched(TouchEvent sceneTouchEvent, float touchAreaLocalX, float touchAreaLocalY) {
				if(sceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					GameScene.this.mPlayerShip.fireLaserBeam();
				}
				return true;
			}
		};
		text.setPosition(text.getWidth() / 2, text.getHeight() / 2);
		this.mGameHUD.registerTouchArea(text);
		this.mGameHUD.attachChild(text);
		
		this.mCamera.setHUD(this.mGameHUD);
	}
}
